﻿using System;
using System.Threading;
using Polenter.Mcm;

namespace McmWinFormsApplication1.Components
{
    public class ReverseTextCommand : Command<ReverseTextCommandArgument, ReverseTextCommandResult>
    {
        public ReverseTextCommand(ReverseTextCommandArgument argument)
            : base(argument)
        {
        }

        /// <summary>
        ///     try...catch is implemented in the base Command. If an error occurs, the Error property is set.
        ///     The whole environment required by the command should be defined in the ReverseTextCommandArgument.
        ///     It makes unit tests possible.
        /// </summary>
        protected override void ExecuteCore()
        {
            // simulate lengthy operation
            Thread.Sleep(2000);

            if (string.IsNullOrEmpty(Argument.InputText))
            {
                throw new InvalidOperationException("Please define the input text.");
            }

            char[] a = Argument.InputText.ToCharArray();
            Array.Reverse(a);
            Result = new ReverseTextCommandResult();
            Result.OutputText = new string(a);
        }
    }
}